package com.fsz.pachong.pachong.controller;

import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.fsz.pachong.pachong.entity.Record;
import com.fsz.pachong.pachong.service.impl.RecordServiceImpl;
import com.fsz.pachong.pachong.util.EasyPoiExcelUtils;
import com.fsz.pachong.pachong.util.PachongUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 爬信息
 * @Author:
 * @Date: 2021/7/12
 * @Version: V1.0
 */
@Controller
@RequestMapping("/record/")
public class RecordController {

    @Autowired
    private RecordServiceImpl recordService;

    @RequestMapping("/seize")
    @ResponseBody
    public void seize(HttpServletResponse response) {

      List<Record> recordList = PachongUtil.pachong(PachongUtil.URL,"UTF-8");
      if(!CollectionUtils.isEmpty(recordList)) {
          for (int i = 0; i < recordList.size(); i++) {
              recordService.save(recordList.get(i));
          }
      }
        EasyPoiExcelUtils.exportExcel(recordList, null, "", Record.class,
                "爬取数据", ExcelType.XSSF, response, 1);
    }
}
